package org.pinda.network.packets;

import java.io.DataOutputStream;
import java.io.IOException;

import org.pinda.network.ConnectionType;
import org.pinda.network.util.Stream;

public class PacketUtils {
	
	/**
	 * Sends a packet on the stream
	 * @param outStream the outStream to write on
	 * @param packet The packet to send
	 * @throws IOException thrown when a OutputStream exception has been thrown
	 */
	public static void writePacket(DataOutputStream outStream, IPacket packet) throws IOException {
		outStream.writeInt(packet.getLength());
		outStream.writeShort(packet.getId());
		packet.write(outStream);
	}
	
	/**
	 * Reads a packet form the stream
	 * @param inStream The stream to read from
	 * @return The packet read from the stream
	 * @throws IOException when an packet is invalid
	 */
	public static IPacket readPacket(ConnectionType connectionType, Stream inStream) throws IOException {
		inStream.readInt();
		int id = inStream.readShort();
		IPacket packet = createPacket(connectionType, id);
		if(packet == null)
			throw new IOException("Invalid Protocol Packet");
		packet.read(inStream);
		return packet;
	}
	
	/**
	 * A factory method to create the packets
	 * @param connectionType the type of connection
	 * @param id the id of the packet
	 * @return
	 */
	private static IPacket createPacket(ConnectionType connectionType, int id) {
		switch(connectionType) {
		
		case GAME:
			switch(id) {
			}
			
		case SERVER_BROWSER:
			switch(id) {
			}
			
		case UPDATE:
			switch(id) {
			}
			
		default:
			return null;
		}
	}

}
